var class_num=location.search;
class_num=class_num.split('?')[1];
if (islogin()) {

    $.post({
        url: '../../requests/teacher/select_section.php',
        data: {'class': class_num},
        async: false,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                var dom1 = "<option value='"+data[i].num+"'>"+data[i].num+"."+data[i].name+"</option>";
                $("select[name='sec']").append(dom1);
            }
        }
    });

    $("select[name='sec']").change(function () {
        if ($(this).val()!="空") {
            $("select[name='sub']").find('option').remove();
            $.post({
                url:'../../requests/teacher/select_subsection.php',
                data:{'class':class_num,'sec': $("select[name='sec']").val()},
                async:false,
                success:function (data) {
                    $("select[name='sub']").append("<option value=''>空</option>");
                    for (var i = 0; i < data.length; i++) {
                        var dom1 = "<option value='"+data[i].num+"'>"+data[i].num+"."+data[i].name+"</option>";
                        $("select[name='sub']").append(dom1);
                    }
                }
            });
        }
        else if ($(this).val() == "空") {
            $("select[name='sub']").find('option').remove();
            $("select[name='sub']").append("<option value=''>空</option>");
        }
    });

    $('#btn_add').click(function () {
        if (!checktitle()) {
            $$.alert('标题不能为空');
        }
        else if (!checktime()){
            $$.alert('作业期限应该在现在之后');
        }
        else {
            var fd = new FormData($('#add_homework_form')[0]);
            var mydate = new Date();
            var time = format(mydate, '-');
            fd.append('class', class_num);
            fd.append('time', time);
            $.post({
                url: '../../requests/teacher/add_homework.php',
                data: fd,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data == '1') {
                        $$.alert('发布成功');
                    }
                    else $$.alert('发布失败');
                }

            });
        }
    });
}
else window.location.href='../../index.html';

//格式化时间
function format(Date,str){
    var obj = {
        Y: Date.getFullYear(),
        M: Date.getMonth() + 1,
        D: Date.getDate(),
        H: Date.getHours(),
        Mi: Date.getMinutes(),
        S: Date.getSeconds()
    }
    // 拼接时间 hh:mm:ss
    var time = ' ' +supplement(obj.H) + ':' + supplement(obj.Mi) + ':' + supplement(obj.S);
    // yyyy-mm-dd
    if(str.indexOf('-') > -1){
        return obj.Y + '-' + supplement(obj.M) + '-' + supplement(obj.D) + time;
    }
    // yyyy/mm/dd
    if(str.indexOf('/') > -1){
        return obj.Y + '/' + supplement(obj.M) + '/' + supplement(obj.D) + time;
    }
}
// 位数不足两位补全0
function supplement(nn){
    return nn = nn < 10 ? '0' + nn : nn;
}

//检查title是否为空
function checktitle() {
    var check=false;
    if ($("input[name='title']").val() == "") {
        check=false;
    }
    else if ($("input[name='title']").val() != "") {
        check=true;
    }
    return check;
}

//检查deadline是否小于或等于time
function checktime() {
    var mydate=new Date();
    var time=format(mydate,'-');
    var deadline=$("input[name='deadline']").val();
    var check=false;

    time=new Date(time.replace(/\-/g,'\/'));
    deadline=new Date(deadline.replace(/\-/g,'\/'));
    if (time>=deadline) {
        check=false;
    }
    else check=true;
    return check;

}
